package com.ruoyi.tccs.tccs_major_judge.utils.judgeResult.impl;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.tccs.tccs_blood_estimate.domain.TccsBloodEstimate;
import com.ruoyi.tccs.tccs_display_esimate.domain.TccsDisplayEsimate;
import com.ruoyi.tccs.tccs_major_judge.domain.TccsMajorJudge;
import com.ruoyi.tccs.tccs_major_judge.utils.acquireAll.tccsAcquire;
import com.ruoyi.tccs.tccs_major_judge.utils.judgeResult.tccsResult;
import com.ruoyi.tccs.tccs_pi_estimate.domain.TccsPiEstimate;
import com.ruoyi.tccs.tccs_ri_estimate.domain.TccsRiEstimate;
import com.ruoyi.tccs.tccs_vs_estimate.domain.TccsVsEstimate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class tccsResultImpl implements tccsResult {

    @Autowired
    private tccsAcquire tccsAcquire;
    @Override
    public void JudgeResult(TccsMajorJudge tccsMajorJudge) {
        tccsMajorJudge.setUniqueNum(System.currentTimeMillis() + 1);
        //判断血流方向/颜色
        TccsBloodEstimate BloodEstimate = new TccsBloodEstimate();
        BloodEstimate.setBloodArteries(tccsMajorJudge.getBloodArteries());
        BloodEstimate.setBloodArteriesChild(tccsMajorJudge.getBloodArteriesChild());
        if(tccsMajorJudge.getBloodDirection() != null){
            BloodEstimate.setBloodDirection(tccsMajorJudge.getBloodDirection());
            if(tccsMajorJudge.getBloodColor() != null){
                BloodEstimate.setBloodColor(tccsMajorJudge.getBloodColor());
            }else{
                //未输入任何血颜色
                tccsMajorJudge.setBloodColorLevel("❌");
                tccsMajorJudge.setBloodColorSignificance("因未输入血信号颜色，暂无结论");
            }
            List<TccsBloodEstimate> BloodEstimates = tccsAcquire.acquireBlood(BloodEstimate);
            //把判断结果写入数据库
            if(BloodEstimates.size() > 0){
                tccsMajorJudge.setBloodDirectionLevel(BloodEstimates.get(0).getBloodDirectionLevel());
                tccsMajorJudge.setBloodColorLevel(BloodEstimates.get(0).getBloodColorLevel());
                if(BloodEstimates.get(0).getBloodColorSignificance() != null){
                    tccsMajorJudge.setBloodColorSignificance(BloodEstimates.get(0).getBloodColorSignificance());
                }
                tccsMajorJudge.setBloodColorSignificance("当前情况暂无结论，请参考：血流信号颜色-是否正常");
            }
        }else{
            //未输入任何血方向
            tccsMajorJudge.setBloodDirectionLevel("❌");
        }
        if("icat".equals(tccsMajorJudge.getBloodArteries())){
            tccsMajorJudge.setBloodDirectionLevel("-");
            tccsMajorJudge.setBloodColorLevel("-");
            tccsMajorJudge.setBloodColorSignificance("当前情况不考虑血流方向和血流颜色");
        }
        //判断vs血流速度
        TccsVsEstimate vsEstimate = new TccsVsEstimate();
        vsEstimate.setBloodArteries(tccsMajorJudge.getBloodArteries());
        List<TccsVsEstimate> VsEstimates = tccsAcquire.acquireVs(vsEstimate);
        if(VsEstimates.size() > 0){
            if(tccsMajorJudge.getBloodVs() != null){
                //vs<正常值低限
                if(tccsMajorJudge.getBloodVs() < VsEstimates.get(0).getPeakvs()){
                    tccsMajorJudge.setBloodVsLevel(VsEstimates.get(0).getPeakvsLevel());
                    tccsMajorJudge.setBloodVsSignificance(VsEstimates.get(0).getPeakvsSignificance());
                    //vs正常值范围
                }else if(VsEstimates.get(0).getMinPeakVs() <= tccsMajorJudge.getBloodVs() && tccsMajorJudge.getBloodVs() <= VsEstimates.get(0).getMaxPeakVs()){
                    tccsMajorJudge.setBloodVsLevel(VsEstimates.get(0).getPeakVsLevel());
                    tccsMajorJudge.setBloodVsSignificance("当前情况暂无结论，请参考：Vs血流速度-是否正常");
                    //正常值高限<Vs<狭窄时流速低限（cm/s）
                }else if(VsEstimates.get(0).getPeakVs() < tccsMajorJudge.getBloodVs() && tccsMajorJudge.getBloodVs()< VsEstimates.get(0).getNarrorVs()){
                    tccsMajorJudge.setBloodVsLevel(VsEstimates.get(0).getPeakNarrorLevel());
                    tccsMajorJudge.setBloodVsSignificance(VsEstimates.get(0).getPeakNarrorSignificance());
                    //<=vs<= 轻度
                }else if(VsEstimates.get(0).getMinNarrorA() <= tccsMajorJudge.getBloodVs() && tccsMajorJudge.getBloodVs()<= VsEstimates.get(0).getMaxNarrorA()){
                    tccsMajorJudge.setBloodVsLevel(VsEstimates.get(0).getNarrorALevel());
                    tccsMajorJudge.setBloodVsSignificance("当前情况暂无结论，请参考：Vs血流速度-是否正常");
                    //<vs< 中度
                }else if(VsEstimates.get(0).getMinNarrorB() < tccsMajorJudge.getBloodVs() && tccsMajorJudge.getBloodVs()< VsEstimates.get(0).getMaxNarrorB()){
                    tccsMajorJudge.setBloodVsLevel(VsEstimates.get(0).getNarrorBLevel());
                    tccsMajorJudge.setBloodVsSignificance("当前情况暂无结论，请参考：Vs血流速度-是否正常");
                    //vs<= 重度
                }else if( VsEstimates.get(0).getMinNarrorC() <= tccsMajorJudge.getBloodVs()){
                    tccsMajorJudge.setBloodVsLevel(VsEstimates.get(0).getNarrorCLevel());
                    tccsMajorJudge.setBloodVsSignificance("当前情况暂无结论，请参考：Vs血流速度-是否正常");
                }else{
                    tccsMajorJudge.setBloodVsLevel("❌");
                    tccsMajorJudge.setBloodVsSignificance("错误，无结论");
                }
            }else{
                tccsMajorJudge.setBloodVsLevel("❌");
                tccsMajorJudge.setBloodVsSignificance("因未输入Vs值，暂无结论");
            }
        }
        //判断PI搏动指数
        TccsPiEstimate piEstimate = new TccsPiEstimate();
        piEstimate.setBloodArteries(tccsMajorJudge.getBloodArteries());
        if(!("oa".equals(tccsMajorJudge.getBloodArteries())) && !("acoa".equals(tccsMajorJudge.getBloodArteries())) && !("pcoa".equals(tccsMajorJudge.getBloodArteries()))){
            piEstimate.setBloodArteries("all");
        }
        List<TccsPiEstimate> PiEstimates = tccsAcquire.acquirePi(piEstimate);
        if(tccsMajorJudge.getPi() != null){
            if(PiEstimates.size() > 0){
                //pi<正常值低限
                if(PiEstimates.get(0).getMinPi() != null && tccsMajorJudge.getPi() < PiEstimates.get(0).getMinPi()){
                    tccsMajorJudge.setPiLevel(PiEstimates.get(0).getPiLevel());
                    tccsMajorJudge.setPiSignificance(PiEstimates.get(0).getPiSignificance());
                    //正常值低限<=pi<=正常值高限
                }else if(PiEstimates.get(0).getMinPiMaxMi()!= null && PiEstimates.get(0).getMinPiMaxMa() != null && PiEstimates.get(0).getMinPiMaxMi() <= tccsMajorJudge.getPi() && tccsMajorJudge.getPi() <= PiEstimates.get(0).getMinPiMaxMa() ){
                    tccsMajorJudge.setPiLevel(PiEstimates.get(0).getMinPiMaxLevel());
                    tccsMajorJudge.setPiSignificance("当前情况暂无结论，请参考：PI搏动指数-是否正常");
                    //正常值高限<pi
                }else if(PiEstimates.get(0).getMaxPi()!= null && PiEstimates.get(0).getMaxPi() < tccsMajorJudge.getPi()){
                    tccsMajorJudge.setPiLevel(PiEstimates.get(0).getMaxPiLevel());
                    tccsMajorJudge.setPiSignificance(PiEstimates.get(0).getMaxPiSignificance());
                }else {
                    tccsMajorJudge.setPiLevel("❌");
                    tccsMajorJudge.setPiSignificance("错误，无结论");
                }
            }
        }else{
            //此处是没有输入pi值
            tccsMajorJudge.setPiLevel("❌");
            tccsMajorJudge.setPiSignificance("因未输入PI值，暂无结论");
        }
        //判断RI搏动指数
        TccsRiEstimate riEstimate = new TccsRiEstimate();
        riEstimate.setBloodArteries(tccsMajorJudge.getBloodArteries());
        if(!("oa".equals(tccsMajorJudge.getBloodArteries())) && !("acoa".equals(tccsMajorJudge.getBloodArteries())) && !("pcoa".equals(tccsMajorJudge.getBloodArteries()))){
            riEstimate.setBloodArteries("all");
        }
        List<TccsRiEstimate> RiEstimates = tccsAcquire.acquireRi(riEstimate);
        if(tccsMajorJudge.getRi() != null){
            if(RiEstimates.size() > 0){
                //ri<正常值低限
                if(RiEstimates.get(0).getMinRi() != null && tccsMajorJudge.getRi() < RiEstimates.get(0).getMinRi()){
                    tccsMajorJudge.setRiLevel(RiEstimates.get(0).getMinRiLevel());
                    tccsMajorJudge.setRiSignificance(RiEstimates.get(0).getMinRiSignificance());
                //正常值低限<=ri<=正常值高限
                }else if(RiEstimates.get(0).getMinRiMaxMi() != null && RiEstimates.get(0).getMinRiMaxMa() != null && RiEstimates.get(0).getMinRiMaxMi() <= tccsMajorJudge.getRi() && tccsMajorJudge.getRi() <= RiEstimates.get(0).getMinRiMaxMa()){
                    tccsMajorJudge.setRiLevel(RiEstimates.get(0).getMinRiMaxLevel());
                    tccsMajorJudge.setRiSignificance("当前情况暂无结论，请参考：RI阻力指数-是否正常");
                //正常值高限<ri
                }else if(RiEstimates.get(0).getMaxRi() != null && RiEstimates.get(0).getMaxRi() < tccsMajorJudge.getRi()  ){
                    tccsMajorJudge.setRiLevel(RiEstimates.get(0).getMaxRiLevel());
                    tccsMajorJudge.setRiSignificance(RiEstimates.get(0).getMaxRiSignificance());
                }else{
                    tccsMajorJudge.setRiLevel("❌");
                    tccsMajorJudge.setRiSignificance("错误，无结论");
                }
            }
        }else {
            //此处是没有输入ri值
            tccsMajorJudge.setRiLevel("❌");
            tccsMajorJudge.setRiSignificance("因未输入Ri值，暂无结论");
        }
        //显示情况
        if(tccsMajorJudge.getDisplay() != null){
            TccsDisplayEsimate displayEsimate = new TccsDisplayEsimate();
            displayEsimate.setBloodArteries(tccsMajorJudge.getBloodArteries());
            displayEsimate.setDisplaySituation(tccsMajorJudge.getDisplay());
            List<TccsDisplayEsimate> DisplayEsimates = tccsAcquire.acquireDisplay(displayEsimate);
            if(DisplayEsimates.size() > 0){
                tccsMajorJudge.setDisplaySignificance(DisplayEsimates.get(0).getClinicalSignificance());
            }else{
                tccsMajorJudge.setDisplaySignificance("暂无结论");
            }


        }
    }
}
